<template>
  <div class="app-container">
    <div>
      <el-select
        size="small"
        v-model="listQuery.AreaCode"
        placeholder="区域"
        clearable
        style="width: 150px;margin-right: 15px;"
        class="filter-item"
        @change="AreaSearch"
      >
        <el-option
          v-for="item in areaDatas"
          :key="item.AreaCode"
          :label="item.AreaName"
          :value="item.AreaCode"
        ></el-option>
      </el-select>
      <el-select
        size="small"
        v-model="listQuery.ProvinceCode"
        placeholder="省份"
        clearable
        style="width: 150px;margin-right: 15px;"
        class="filter-item"
        @change="ProvinceSearch"
      >
        <el-option
          v-for="item in provinceDatas"
          :key="item.ProvinceCode"
          :label="item.ProvinceName"
          :value="item.ProvinceCode"
        ></el-option>
      </el-select>
      <el-select
        size="small"
        v-model="listQuery.CityCode"
        placeholder="城市"
        clearable
        style="width: 250px;margin-right: 15px;"
        class="filter-item"
        @change="CitySearch"
      >
        <el-option
          v-for="item in cityDatas"
          :key="item.CityCode"
          :label="item.CityName"
          :value="item.CityCode"
        ></el-option>
      </el-select>
    </div>
    <div style="padding-top:20px;">
      <el-input
        v-model.trim="listQuery.DealerCode"
        size="small"
        placeholder="经销商代码"
        style="width: 150px;margin-right: 15px;"
        class="filter-item"
        @keyup.enter.native="getList(1)"
      />
      <el-input
        v-model.trim="listQuery.DealerName"
        size="small"
        placeholder="经销商名称"
        style="width: 400px;margin-right: 15px;"
        class="filter-item"
        @keyup.enter.native="getList(1)"
      />
      <el-input
        v-model.trim="listQuery.ManagementCode"
        size="small"
        placeholder="一级代码"
        style="width: 150px;margin-right: 15px;"
        class="filter-item"
        @keyup.enter.native="getList(1)"
      />
      <el-input
        v-model.trim="listQuery.ManagementName"
        size="small"
        placeholder="管理体系简称"
        style="width: 200px;margin-right: 15px;"
        class="filter-item"
        @keyup.enter.native="getList(1)"
      />
      <el-button
        style="margin-right: 15px;"
        type="primary"
        @click="getList(1)"
        size="small"
        icon="el-icon-search"
      >查询</el-button>
      <el-button type="primary" @click="handleAdd" size="small" icon="el-icon-plus">添加</el-button>
    </div>
    <div>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="views"
        style="width: 100%;margin-top:30px;"
        border
      >
        <el-table-column label="区域" width="70">
          <template slot-scope="scope">{{ scope.row.AreaName }}</template>
        </el-table-column>
        <el-table-column label="省份" width="70">
          <template slot-scope="scope">{{ scope.row.ProvinceName }}</template>
        </el-table-column>
        <el-table-column label="城市">
          <template slot-scope="scope">{{ scope.row.CityName }}</template>
        </el-table-column>
        <el-table-column label="经销商代码" width="110">
          <template slot-scope="scope">{{ scope.row.DealerInfo.DealerCode }}</template>
        </el-table-column>
        <el-table-column label="经销商名称" width="310">
          <template slot-scope="scope">{{ scope.row.DealerInfo.DealerName }}</template>
        </el-table-column>
        <el-table-column label="业务状态" width="80">
          >
          <template slot-scope="scope">{{ scope.row.DealerInfo.BusinessNature }}</template>
        </el-table-column>
        <el-table-column label="经营性质" width="80">
          <template slot-scope="scope">{{ scope.row.DealerInfo.BusinessType }}</template>
        </el-table-column>
        <el-table-column label="一级代码" width="80">
          <template slot-scope="scope">{{ scope.row.DealerInfo.ManagementCode }}</template>
        </el-table-column>
        <el-table-column label="管理体系简称" width="120">
          <template slot-scope="scope">{{ scope.row.DealerInfo.ManagementName }}</template>
        </el-table-column>
        <el-table-column align="center" width="200" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleEdit(scope.row.DealerInfo)"
              icon="el-icon-edit"
            >编辑</el-button>
            <el-button
              type="text"
              size="mini"
              @click="handleDelete(scope.row.DealerInfo)"
              icon="el-icon-delete"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top:30px">
      <go-page
        v-model="listQuery.Page"
        :pageSize="listQuery.PageSize"
        :total="listQuery.Total"
        @pagination="getList()"
      />
    </div>

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogtext"
      width="60%"
      top="20vh"
      :close-on-click-modal="false"
    >
      <el-form
        :model="temp"
        ref="dealerDialog"
        label-position="right"
        label-width="100px"
        style="width: 90%;"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="区域">
              <el-select
                style="width:130px;"
                v-model="temp.AreaCode"
                placeholder="请选择"
                @change="AreaChanage"
              >
                <el-option
                  v-for="item in areaTempDatas"
                  :key="item.AreaCode"
                  :label="item.AreaName"
                  :value="item.AreaCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="省份">
              <el-select
                style="width:130px;"
                v-model="temp.ProvinceCode"
                placeholder="请选择"
                @change="ProvinceChange"
              >
                <el-option
                  v-for="item in provinceTempDatas"
                  :key="item.ProvinceCode"
                  :label="item.ProvinceName"
                  :value="item.ProvinceCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="城市">
              <el-select style="width:100%;" v-model="temp.CityCode" placeholder="请选择">
                <el-option
                  v-for="item in cityTempDatas"
                  :key="item.CityCode"
                  :label="item.CityName"
                  :value="item.CityCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="经销商代码">
              <el-input
                @blur="DealerCodeFocus"
                v-model.trim="temp.DealerCode"
                placeholder="请填写经销商代码"
              />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="经销商名称">
              <el-input v-model.trim="temp.DealerName" placeholder="请填写单位名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务状态">
              <el-input v-model.trim="temp.BusinessNature" placeholder="请填写业务状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营性质">
              <el-input v-model.trim="temp.BusinessType" placeholder="请填写经营性质" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="一级代码">
              <el-input disabled v-model.trim="temp.ManagementCode" placeholder="请填写一级代码" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="管理体系简称">
              <el-input v-model.trim="temp.ManagementName" placeholder="请填写管理体系简称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div style="text-align:right;">
        <el-button type="danger" @click="close">关闭</el-button>
        <el-button type="primary" @click="confirmData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import { reqGetAreaCitys } from "@/api/areacity";
import {
  reqGetDealerInfos,
  reqAddDealerInfo,
  reqUpdateDealerInfo
} from "@/api/dealerInfo";

import GoPage from "./components/gopage";

export default {
  components: {
    GoPage
  },
  data() {
    return {
      listQuery: {
        Page: 1,
        PageSize: 7,
        Total: 0,
        OrderBy: "CreateTime",
        IsAsc: true,
        AreaCode: null,
        ProvinceCode: null,
        CityCode: null,
        DealerCode: "",
        DealerName: "",
        ManagementCode: "",
        ManagementName: ""
      },
      query: {
        Page: 1,
        PageSize: 10000,
        Total: 0,
        OrderBy: "AreaCode",
        IsAsc: true,
        TypeLevel: -1,
        AreaCode: -1,
        AreaName: "",
        ProvinceCode: -1,
        ProvinceName: "",
        CityCode: -1,
        CityName: ""
      },
      temp: {
        ID: null,
        AreaCode: 101,
        ProvinceCode: 10001,
        CityCode: 10000001,
        DealerCode: "",
        BusinessType: "",
        DealerName: "",
        BusinessNature: "",
        ManagementCode: "",
        ManagementName: "",
        InvestCode: "",
        InvestName: ""
      },
      views: [],
      areaCitys: [],
      areaDatas: [],
      provinceDatas: [],
      cityDatas: [],
      areaTempDatas: [],
      provinceTempDatas: [],
      cityTempDatas: [],
      dialogtext: "新增信息",
      loading: true,
      isEidt: false,
      dialogVisible: false
    };
  },
  computed: {
    // 计算属性，只要相关联的值没有改变，直接返回，不再进行计算，与方法区别，缓存
  },
  created() {
    // 实例创建完成后执行的代码
    this.getList(1);
    this.searchInit();
  },
  methods: {
    // -----------------------------------------------初始化模块
    // list
    async getList(page1) {
      this.loading = true;
      if (page1 === 1) {
        this.listQuery.Page = 1;
      }
      let res = await reqGetDealerInfos(this.listQuery);
      if (res.code === 200) {
        this.views = res.data.Items;
        this.listQuery.Total = res.data.TotalItems;
        this.loading = false;
      }
    },
    // 区、省、市数据初始化
    async searchInit() {
      let res = await reqGetAreaCitys(this.query);
      if (res.code === 200) {
        this.areaCitys = res.data.Items;
        this.areaDatas = this.areaCitys.filter(s => s.TypeLevel === 1);
        this.provinceDatas = this.areaCitys.filter(s => s.TypeLevel === 2);
        this.cityDatas = this.areaCitys.filter(s => s.TypeLevel === 3);

        this.areaTempDatas = this.areaCitys.filter(s => s.TypeLevel === 1);
        if (!this.isEidt) this.temp.AreaCode = this.areaTempDatas[0].AreaCode;
      }
    },
    // -----------------------------------------------搜索模块
    // 区域下拉搜索
    AreaSearch() {
      if (this.listQuery.AreaCode) {
        this.provinceDatas = this.areaCitys.filter(
          s => s.TypeLevel === 2 && s.AreaCode === this.listQuery.AreaCode
        );
        this.cityDatas = this.areaCitys.filter(
          s => s.TypeLevel === 3 && s.AreaCode === this.listQuery.AreaCode
        );
      } else {
        this.provinceDatas = this.areaCitys.filter(s => s.TypeLevel === 2);
        this.cityDatas = this.areaCitys.filter(s => s.TypeLevel === 3);
      }
      this.listQuery.ProvinceCode = null;
      this.listQuery.CityCode = null;
      this.getList(1);
    },
    // 省份下拉搜索
    ProvinceSearch() {
      if (this.listQuery.ProvinceCode) {
        this.cityDatas = this.areaCitys.filter(
          s =>
            s.TypeLevel === 3 && s.ProvinceCode === this.listQuery.ProvinceCode
        );
      } else {
        if (this.listQuery.AreaCode)
          this.cityDatas = this.areaCitys.filter(
            s => s.TypeLevel === 3 && s.AreaCode === this.listQuery.AreaCode
          );
        else this.cityDatas = this.areaCitys.filter(s => s.TypeLevel === 3);
      }
      this.listQuery.CityCode = null;
      this.getList(1);
    },
    // 城市下拉搜索
    CitySearch() {
      this.getList(1);
    },
    // 切换区域下拉-新增/编辑
    AreaChanage() {
      this.provinceTempDatas = this.areaCitys.filter(
        s => s.TypeLevel === 2 && s.AreaCode === this.temp.AreaCode
      );
      this.temp.ProvinceCode = this.provinceTempDatas[0].ProvinceCode;
      this.ProvinceChange();
    },
    // 切换省份下拉-新增/编辑
    ProvinceChange() {
      this.cityTempDatas = this.areaCitys.filter(
        s => s.TypeLevel === 3 && s.ProvinceCode === this.temp.ProvinceCode
      );
      this.temp.CityCode = this.cityTempDatas[0].CityCode;
    },
    // 经销商代码输入焦点事件
    DealerCodeFocus() {
      this.temp.ManagementCode = this.temp.DealerCode.substr(0, 7);
    },
    // -----------------------------------------------业务操作模块
    restTemp() {
      this.temp = {
        ID: null,
        AreaCode: 101,
        ProvinceCode: 10001,
        CityCode: 10000001,
        DealerCode: "",
        BusinessType: "",
        DealerName: "",
        BusinessNature: "",
        ManagementCode: "",
        ManagementName: "",
        InvestCode: "",
        InvestName: ""
      };
    },
    // 新增数据操作
    handleAdd() {
      this.dialogtext = this.isEidt ? "编辑信息" : "新增信息";
      this.dialogVisible = true;
      this.AreaChanage();
    },
    // 校验数据新增/更新
    confirmData() {
      if (!this.temp.DealerCode) {
        this.$message({
          message: "请输入经销商代码",
          type: "warning",
          duration: 3000
        });
        return;
      } else if (!this.temp.DealerName) {
        this.$message({
          message: "请输入经销商名称",
          type: "warning",
          duration: 3000
        });
        return;
      } else if (!this.temp.ManagementName) {
        this.$message({
          message: "请输入管理体系简称",
          type: "warning",
          duration: 3000
        });
        return;
      }
      if (this.isEidt) this.dealerInfoUpdate();
      else this.dealerInfoAdd();
    },
    // 新增经销商信息数据
    async dealerInfoAdd() {
      let res = await reqAddDealerInfo(this.temp);
      if (res.code === 200) {
        if (res.data) {
          this.$notify({
            title: "成功",
            message: "操作成功",
            type: "success",
            duration: 3000
          });
          this.dialogVisible = false;
          this.getList(1);
          this.restTemp();
        } else {
          this.$notify({
            title: "失败",
            message: res.message,
            type: "error",
            duration: 3000
          });
        }
      }
    },
    // 更新区域信息数据
    handleEdit(row) {
      this.isEidt = true;
      this.dialogVisible = true;
      this.temp = Object.assign({}, row);
      this.AreaChanage();
      this.temp.ProvinceCode = row.ProvinceCode;
      this.ProvinceChange();
      this.temp.CityCode = row.CityCode;
    },
    // 更新区域信息数据
    async dealerInfoUpdate() {
      let res = await reqUpdateDealerInfo(this.temp);
      if (res.code === 200) {
        if (res.data) {
          this.$notify({
            title: "成功",
            message: "操作成功",
            type: "success",
            duration: 3000
          });
          this.dialogVisible = false;
          this.getList(1);
          this.restTemp();
        } else {
          this.$notify({
            title: "失败",
            message: res.message,
            type: "error",
            duration: 3000
          });
        }
      }
    },
    // 删除区域信息数据
    async handleDelete(row) {
      this.$confirm("确认删除此条数据?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          row.IsDelete = true;
          let res = await reqUpdateDealerInfo(row);
          if (res.code === 200) {
            if (res.data) {
              this.$notify({
                title: "成功",
                message: "操作成功",
                type: "success",
                duration: 3000
              });
            } else {
              this.$notify({
                title: "失败",
                message: res.message,
                type: "error",
                duration: 3000
              });
            }
          }
          this.getList(1);
        })
        .catch(err => {
          // console.error(err);
        });
    },
    close() {
      this.dialogVisible = false;
      this.restTemp();
    }
  }
};
</script>